package com.dingwei.superiordiary.utils;

import android.app.Activity;
import android.app.Dialog;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dingwei.superiordiary.R;
import com.lihao.baselibrary.base.BaseActivity;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

/**
 * Created by lihao on 2017/12/26.
 */

public class ShareDialogUtils {


    public static Dialog shareDialog;

    public static void showShareDialog(final Activity context, final String url, final String title, final String img_path, final String description, final String withText, final UMShareListener shareListener){
        shareDialog = new Dialog(context, R.style.Theme_Light_Dialog);

        View view = LayoutInflater.from(BaseActivity.context).inflate(R.layout.share_dialog, null);
        view.measure(0,0);
        TextView tvcancel = (TextView) view.findViewById(R.id.tv_cancel);
        LinearLayout llqqkongjian = (LinearLayout) view.findViewById(R.id.ll_qq_kongjian);
        LinearLayout llqq = (LinearLayout) view.findViewById(R.id.ll_qq);
        LinearLayout llpengyou = (LinearLayout) view.findViewById(R.id.ll_pengyou);
        LinearLayout llweixin = (LinearLayout) view.findViewById(R.id.ll_weixin);

        llweixin.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                shareDialog.dismiss();
                UMWeb web;
                web = new UMWeb(url);
                web.setTitle(title);
                if(TextUtils.isEmpty(img_path)){
                    web.setThumb(new UMImage(context, R.drawable.pic45));
                }else{
                    web.setThumb(new UMImage(context,img_path));
                }
                web.setDescription(description);
                new ShareAction(context)
                        .withText(withText)
                        .withMedia(web)
                        .setPlatform(SHARE_MEDIA.WEIXIN)
                        .setCallback(shareListener).share();
            }
        });
        llpengyou.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                shareDialog.dismiss();
                UMWeb web;
                web = new UMWeb(url);
                web.setTitle(title);
                if(TextUtils.isEmpty(img_path)){
                    web.setThumb(new UMImage(context, R.drawable.pic45));
                }else{
                    web.setThumb(new UMImage(context,img_path));
                }
                web.setDescription(description);
                new ShareAction(context)
                        .withText(withText)
                        .withMedia(web)
                        .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE)
                        .setCallback(shareListener).share();
            }
        });
        llqq.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                shareDialog.dismiss();
                UMWeb web;
                web = new UMWeb(url);
                web.setTitle(title);
                if(TextUtils.isEmpty(img_path)){
                    web.setThumb(new UMImage(context, R.drawable.pic45));
                }else{
                    web.setThumb(new UMImage(context,img_path));
                }
                web.setDescription(description);
                new ShareAction(context)
                        .withText(withText)
                        .withMedia(web)
                        .setPlatform(SHARE_MEDIA.QQ)
                        .setCallback(shareListener).share();
            }
        });
        llqqkongjian.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                shareDialog.dismiss();
                UMWeb web;
                web = new UMWeb(url);
                web.setTitle(title);
                if(TextUtils.isEmpty(img_path)){
                    web.setThumb(new UMImage(context, R.drawable.pic45));
                }else{
                    web.setThumb(new UMImage(context,img_path));
                }
                web.setDescription(description);
                new ShareAction(context)
                        .withText(withText)
                        .withMedia(web)
                        .setPlatform(SHARE_MEDIA.QZONE)
                        .setCallback(shareListener).share();
            }
        });
        tvcancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shareDialog.dismiss();
            }
        });
        shareDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        shareDialog.setContentView(view);
        Window dialogWindow = shareDialog.getWindow();
        //设置dialog在屏幕底部
        dialogWindow.setGravity(Gravity.BOTTOM);
        //设置dialog弹出时的动画效果，从屏幕底部向上弹出
        dialogWindow.setWindowAnimations(R.style.dialogstyle);
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        //获得window窗口的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        //设置窗口宽度为充满全屏
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        //设置窗口高度为包裹内容
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //将设置好的属性set回去
        dialogWindow.setAttributes(lp);
        shareDialog.show();
    }

}
